package com.example.cpoopc.listview_test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.ScrollView;

/**
 * Created by cpoopc on 2015/1/26.
 */
public class VerticalScrollview extends ScrollView{
    private boolean mScrollEnable = true;

    public void setScrollEnable(boolean mEnable) {
        this.mScrollEnable = mEnable;
    }

    public VerticalScrollview(Context context) {
        super(context);
    }

    public VerticalScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float positionX;
    private float positionY;
    
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        final int action = ev.getAction();
//        switch (action)
//        {
//            case MotionEvent.ACTION_DOWN:
//                positionX = ev.getX();
//                positionY = ev.getY();
//                Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false");
//                super.onTouchEvent(ev);
//                break;
//
//            case MotionEvent.ACTION_MOVE:
//                boolean isDown = ev.getY()-positionY>0?true:false;
//                int[] location = new int[2];
//                for (int i = 0;i<getChildCount();i++){
//                    getChildAt(i).getLocationInWindow(location);
//                    Log.d("location", i + ":" + location[1]);
//                }
////                getLocationInWindow(location);
//                if(!isDown && location[1]<=-287){
//                    mScrollEnable = false;
//                    return false;
//                }else if(isDown && !mScrollEnable){
//                    return false;
//                }
//                return super.onTouchEvent(ev); // redirect MotionEvents to ourself
//
//            case MotionEvent.ACTION_CANCEL:
//                Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
//                super.onTouchEvent(ev);
//                break;
//
//            case MotionEvent.ACTION_UP:
//                Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
//                return false;
//
//            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
//        }
//
//        return false;
//    }
    
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//    	Log.d("cp:VerticalScrollview", "dispatchTouchEvent:"+super.dispatchTouchEvent(ev));
//    	return super.dispatchTouchEvent(ev);
//    }
//    
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//    	Log.d("cp:VerticalScrollview", "onInterceptTouchEvent"+super.onInterceptTouchEvent(ev));
//    	return super.onInterceptTouchEvent(ev);
//    }
//    
//    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//        super.onTouchEvent(ev);
//        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
//        return true;
//    }
    boolean giveChild = false;
    public ListView lv;
    
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    	if(giveChild && lv!=null){
    		lv.onTouchEvent(ev);
    		return lv.onTouchEvent(ev);
    	}
    	return super.onTouchEvent(ev);
    }
}
